﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TR.Domain.Entity;
using TR.Domain.Entity.BsItemType;
using TR.Domain.Entity.JH;
using TR.Domain.Repository.Wh_Buy_Order_hRepository;

namespace TR.EFCore.Repository.Wh_Buy_Order_hRepository
{
    public class wh_Buy_Order_hRepository : Iwh_Buy_Order_hRepository
    {
        private readonly MyContext context;

        public wh_Buy_Order_hRepository(MyContext context)
        {
            this.context = context;
        }

        private int fyzts;//分页总条数

        //分页条件查询
        public Task<List<wh_Buy_Order_h>> FenYeSelect(int mys, int dqy, string name)
        {
            //使用linq实现
            var query = context.Wboh.AsQueryable();

            query = query.Where(p => p.is_del == 0);//分页查询条件

            

            return query.OrderByDescending(p => p.create_time).Skip(mys * (dqy - 1)).Take(mys).ToListAsync();//异步获取使用ToListAsync();

        }

        //分页总条数查询
        public async Task<int> FenYeZtsSelect()
        {
            return fyzts;
        }


        //订单详情查询
        public async Task<List<JHXQ>> JHXQTjSelect(string tj)
        {
            var query = context.JHXQ.AsQueryable();

            query = query.Where(p => p.buy_code == tj);//条件

            return query.ToList();
        }

        //进货订单编号查询
        public async Task<wh_Buy_Order_h> OrderBhSelect(string tj)
        {
            var result = await context.Wboh.Where(p => p.buy_code == tj).FirstOrDefaultAsync();
            return result;
        }

        //信息新增
        public async Task<bool> InsertOrder(wh_Buy_Order_h jhzb, List<wh_orderbuy_d> ddmx)
        {
            var query = context;
            try
            {
                var wboh = new wh_Buy_Order_h()
                {
                    buy_code = jhzb.buy_code,
                    buy_date = jhzb.buy_date,
                    supply_code = jhzb.supply_code,
                    link_man = jhzb.link_man,
                    link_way = jhzb.link_way,
                    buy_man = jhzb.buy_man,
                    create_by = jhzb.create_by,
                    create_time = jhzb.create_time,
                    sure_date = jhzb.sure_date,
                    sure_state = '2',
                    remark = jhzb.remark,
                    is_del = 0,

                };

                foreach (var mx in ddmx)
                {
                    var wod = new wh_orderbuy_d()
                    {
                        buy_code = jhzb.buy_code,
                        item_name = mx.item_name,
                        item_code = mx.item_code,
                        model_code = mx.model_code,
                        unit = mx.unit,
                        item_type = "2",
                        buy_num = mx.buy_num,
                        buy_price = mx.buy_price,
                        inbill_num = 0,
                        return_goods_num = 0,
                        is_del = 0,

                    };
                    query.wh_Buy_Order_H.Add(wboh);
                    query.wh_orderbuy_d.Add(wod);
                }
                await context.SaveChangesAsync();
                return true;// 如果SaveChangesAsync没有抛出异常，则认为操作成功
            }
            catch (Exception ex)
            {
                Console.WriteLine("错误" + ex);
                return false;
            }
        }

        //信息修改
        public async Task<bool> UpdateOrder(wh_Buy_Order_h jhzb, List<wh_orderbuy_d> ddmx)
        {
            var query = context;
            try
            {
                var wbohUpdate = query.wh_Buy_Order_H.FirstOrDefault(p => p.buy_code == jhzb.buy_code);
                if (wbohUpdate != null)
                {
                    wbohUpdate.buy_date = jhzb.buy_date;
                    wbohUpdate.supply_code = jhzb.supply_code;
                    wbohUpdate.link_man = jhzb.link_man;
                    wbohUpdate.buy_man = jhzb.buy_man;
                    wbohUpdate.link_way = jhzb.link_way;
                    wbohUpdate.create_by = jhzb.create_by;
                    wbohUpdate.create_time  = jhzb.create_time;
                    wbohUpdate.sure_man = jhzb.sure_man;
                    wbohUpdate.sure_date = jhzb.sure_date;
                    wbohUpdate.sure_state = '2';
                    wbohUpdate.remark = jhzb.remark;
                    wbohUpdate.is_del = 0;
                    

                    //var wbohdel = query.wh_orderbuy_d.Where(p => p.buy_code == jhzb.buy_code).ToList();
                    //query.wh_orderbuy_d.RemoveRange(wbohdel);

                    foreach (var mx in ddmx)
                    {
                        var wod = new wh_orderbuy_d()
                        {
                            buy_code = jhzb.buy_code,
                            item_name = mx.item_name,
                            item_code = mx.item_code,
                            model_code = mx.model_code,
                            unit = mx.unit,
                            item_type = "2",
                            buy_num = mx.buy_num,
                            buy_price = mx.buy_price,
                            inbill_num = 0,
                            return_goods_num = 0,
                            is_del = 0,
                        };
                        query.wh_Buy_Order_H.Update(wbohUpdate);
                        query.wh_orderbuy_d.Add(wod);
                    }
                    await query.SaveChangesAsync();
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("错误信息"+ex.Message);
                return false;
            }
        }

        //供应商下拉框
        public Task<List<bs_Supply>> bs_SupplieSelect()
        {
            var query = context.bs_Supply.AsQueryable();

            query = query.Where(p => p.is_del == 0);

            return query.ToListAsync();
        }

        //供应商信息查询联系人和联系方式
        public async Task<bs_Supply> bs_SupplyTjSelect(string tj)
        {
            var query = context.bs_Supply;

            var result = await query.Where(p => p.supply_code == tj).FirstOrDefaultAsync();

            return result;
        }

        //货品信息
        public async Task<List<bs_Item>> BsItemSelect(int mys, int dqy, string? tj)
        {
            var query = context.bs_Item.AsQueryable();
            query = query.Where(p => p.is_del == 0);

            if (!string.IsNullOrEmpty(tj))
            {
                query = query.Where(p => EF.Functions.Like(p.item_code, $"%{tj}%") || EF.Functions.Like(p.item_name, $"%{tj}%"));
            }

            var result = await query.OrderBy(p => p.id).Skip(mys * (dqy-1)).Take(mys).ToListAsync();

            fyzts = await query.CountAsync();

            return result;
        }

        //货品总数
        public async Task<int> ShuLiang()
        {
            return fyzts;
        }
    }
}
